#encoding:gbk
__author__ = 'administrator'
import os

import sys
import MySQLdb


if __name__ == '__main__':
    reload(sys)
    sys.setdefaultencoding('gbk')
    conn = MySQLdb.connect(host = "192.168.15.7", user ="mars",passwd = "mars123",db="", port=3306, charset="utf8")
    cur = conn.cursor()
    conn2 = MySQLdb.connect(host = "192.168.15.7", user ="mars",passwd = "mars123",db="", port=3306, charset="utf8")
    cur2 = conn2.cursor()
    cur3=conn2.cursor()
    conn.select_db('test')
    conn2.select_db('test')
    count6=0
    countflag=0
    sql='select * from tmp548   '
    count = cur.execute(sql)
    sql1="SELECT * from yy_line_daily_0408 where work_date=%s and line_name = %s and daily_type=1"
    #sql2="select * from yy_line_daily where work_date=%s"
    sql3="update  yy_line_daily_0408 set KILOMETER=%s,KILOMETER_EMPTY='0', KILOMETER_DIESEL=%s,KILOMETER_TROLLEYBUS=%s,KILOMETER_ELECTRIC=%s,ENERGY_CONSUMPTION_DIESEL=%s,ENERGY_CONSUMPTION_ELECTRIC=%s,PERSON_TIME_TOTAL=%s,PERSON_TIME_PREFERENTIAL=%s,PERSON_TIME_FREE=%s,INCOME_TOTAL=%s,INCOME_PREFERENTIAL=%s,INCOME_FREE=%s where work_date=%s and LINE_NAME=%s and daily_type=1"
    sql4="update  yy_line_daily_0408 set KILOMETER=%s,KILOMETER_EMPTY='0', KILOMETER_DIESEL=%s,KILOMETER_TROLLEYBUS=%s,KILOMETER_ELECTRIC=%s,ENERGY_CONSUMPTION_DIESEL=%s,ENERGY_CONSUMPTION_ELECTRIC=%s,PERSON_TIME_TOTAL=%s,PERSON_TIME_PREFERENTIAL=%s,PERSON_TIME_FREE=%s,INCOME_TOTAL=%s,INCOME_PREFERENTIAL=%s,INCOME_FREE=%s where work_date=%s and LINE_NAME=%s and daily_type=1 and id=%s"

    print count

    result = cur.fetchall()
    for r in result:
        #print r[0], r[1] ,r[11],r[12],r[34],r[16],r[36]
        count2=cur2.execute(sql1,(r[1],r[0]))
        result2 = cur2.fetchall()
        for r2 in result2:
            if count2 == 1:
                testvar=r[11]+r[12]+r[34]
                testvar=round(float(r[11])+float(r[12])+float(r[34]),2)
                cur3.execute(sql3,(str(testvar),r[11],r[12],r[34],r[16],r[36],r[5],r[6],r[7],r[8],r[9],r[10],r[1],r[0]))
                print count6
                count6=count6+1
            else:
                if countflag == 0:
                    testvar=r[11]+r[12]+r[34]
                    testvar=round(float(r[11])+float(r[12])+float(r[34]),2)
                    cur3.execute(sql4,(str(testvar),r[11],r[12],r[34],r[16],r[36],r[5],r[6],r[7],r[8],r[9],r[10],r[1],r[0],r2[0]))
                    countflag = 1
                else:
                    cur3.execute(sql4,(0,0,0,0,0,0,0,0,0,0,0,0,r[1],r[0],r2[0]))
        countflag = 0

        #result2 = cur2.fetchall()
        #conn2.commit()
    conn.commit()
    cur.close()
    conn.close()
    conn2.commit()
    cur2.close()
    conn2.close()